package com.youlu.campus.web.jinshuju;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.entity.jinshuju.dto.JinUserJoinInfoDTO;
import com.youlu.campus.entity.jinshuju.dto.JinUserParticipateDTO;
import com.youlu.campus.entity.jinshuju.dto.JinUserResInfoDTO;
import com.youlu.campus.entity.jinshuju.dto.QueryUserJinShuJuResultDTO;
import com.youlu.campus.entity.jinshuju.dto.UpdateJinUserInfoDTO;
import com.youlu.campus.entity.jinshuju.vo.JinUserInfoQueryVo;
import com.youlu.campus.entity.jinshuju.vo.QueryIndexVO;
import com.youlu.campus.service.jinshuju.JinShuJuUserActivityService;
import com.youlu.campus.service.utils.WebTokenUtils;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author : [dengbo]
 * @className : JinUserActivity
 * @description : [金数据用户活动控制器]
 * @createTime : [2023/6/13 16:36]
 */
@Slf4j
@RestController
public class JinUserActivityController {

    @Autowired
    private JinShuJuUserActivityService jinShuJuUserActivityService;

    /**
     * 查询报名信息
     * 参数：request - 金数据报名信息
     */
    @RequestMapping(value = "/public/jin/query/join/info", method = RequestMethod.POST)
    public QResult queryJoinInfo(@Validated @RequestBody JinUserInfoQueryVo queryVo) {
        JinUserJoinInfoDTO data = jinShuJuUserActivityService.getJoinInfo(queryVo);
        if (data.getHasJoin()) {
            data.setUserToken(WebTokenUtils.signToken(data.getOpenId()));
        }
        return QResult.success(data);
    }

    /**
     * 校验是否有用户数据
     * 参数：request - 金数据报名信息
     */
    @RequestMapping(value = "/public/jin/check/user/info", method = RequestMethod.POST)
    public QResult checkUserInfo(@Validated @RequestBody JinUserInfoQueryVo queryVo, HttpServletRequest request) {
        String channelCode = request.getHeader("channel");
        queryVo.setChannel(channelCode);
        JinUserResInfoDTO data = jinShuJuUserActivityService.queryUserInfo(queryVo);
        log.info(":>>> 校验是否有用户数据结果:{}", JSON.toJSON(data));
        if (data.getHasUserInfo()) {
            data.setUserToken(WebTokenUtils.signToken(data.getOpenId()));
        }
        return QResult.success(data);
    }

    /**
     * 登录添加报名
     * 参数：request - 金数据报名信息
     */
    @RequestMapping(value = "/mini-app/jin/check/user/info", method = RequestMethod.POST)
    public QResult loginParticipate(@Validated @RequestBody JinUserInfoQueryVo queryVo, HttpServletRequest request) {
        queryVo.setUserPO(RequestContext.getCurUser());
        String channelCode = request.getHeader("channel");
        queryVo.setChannel(channelCode);
        JinUserResInfoDTO data = jinShuJuUserActivityService.queryUserInfo(queryVo);
        log.info(":>>> 校验是否有用户数据结果:{}", JSON.toJSON(data));
        if (data.getHasUserInfo()) {
            data.setUserToken(WebTokenUtils.signToken(data.getOpenId()));
        }
        return QResult.success(data);
    }

    /**
     * 用户报名
     * 参数：request - 金数据用户报名
     */
    @RequestMapping(value = "/public/jin/user/participate", method = RequestMethod.POST)
    public QResult addParticipate(@Validated @RequestBody JinUserParticipateDTO queryVo, HttpServletRequest request) {
        String channelCode = request.getHeader("channel");
        queryVo.setChannel(channelCode);
        JinUserResInfoDTO data = jinShuJuUserActivityService.addUserParticipate(queryVo);
        if (data.getHasUserInfo()) {
            data.setUserToken(WebTokenUtils.signToken(data.getOpenId()));
        }
        return QResult.success(data);
    }

    /**
     * 查询报名信息
     */
    @PostMapping("/public/jin/query/sign-up")
    public QResult queryIndexSearch(@RequestBody QueryIndexVO queryIndex, HttpServletRequest request) {
        queryIndex.setRemoteIp(request.getRemoteAddr());
        QueryUserJinShuJuResultDTO dto = jinShuJuUserActivityService.queryUserSignUpInfo(queryIndex);
        if (dto.getJoined()) {
            dto.setUserToken(WebTokenUtils.signToken(dto.getOpenId()));
        }
        return QResult.success(dto);
    }

    /**
     * 查询报名信息（token存在情况）
     */
    @PostMapping("/mini-app/jin/query/sign-up")
    public QResult queryTokenIndexSearch(@RequestBody QueryIndexVO queryIndex, HttpServletRequest request) {
        queryIndex.setRemoteIp(request.getRemoteAddr());
        queryIndex.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(jinShuJuUserActivityService.queryUserSignUpInfo(queryIndex));
    }

    /**
     * 查询活动报名海报邀请信息
     */
    @PostMapping("/mini-app/jin/invite")
    public QResult inviteActivity(String activityId, String platform) {
        return QResult.success(jinShuJuUserActivityService.findInviteInfoByActivityId(activityId,
                platform, RequestContext.getCurUser()));
    }

    /**
     * 更新用户信息（token存在情况）
     */
    @PostMapping("/mini-app/jin/update/user-info")
    public QResult updateUserInfo(@RequestBody UpdateJinUserInfoDTO updateUserInfo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        updateUserInfo.setCurrentUser(RequestContext.getCurUser());
        jinShuJuUserActivityService.updateUserInfo(updateUserInfo);
        return QResult.success(null);
    }

}